package com.shrct.businessprocess.project;

import com.google.common.base.Objects;
import com.shrct.businessprocess.common.CurrencyType;
import com.shrct.businessprocess.project.RCExchangeRate;
import com.shrct.businessprocess.project.impl.RCExchangeRateImpl;
import edu.fudan.mylang.common.NumberExtensions;
import edu.fudan.mylang.pf.BaseDataType;
import javax.persistence.CascadeType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import org.eclipse.xtext.xbase.lib.DoubleExtensions;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;

public class RCProjectMoney extends BaseDataType {
  public RCProjectMoney() {
    super();
  }
  
  private Double amount = Double.valueOf(0d);
  
  public Double getAmount() {
    return this.amount;
  }
  
  public RCProjectMoney setAmount(final Double amount) {
    this.amount = amount;
    return this;			
    
  }
  
  @JoinColumn(name = "exchange_id")
  @ManyToOne(targetEntity = RCExchangeRateImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST })
  private RCExchangeRate exchange;
  
  public RCExchangeRate getExchange() {
    return this.exchange;
  }
  
  public RCProjectMoney setExchange(final RCExchangeRate exchange) {
    this.exchange = exchange;
    return this;			
    
  }
  
  public Double getExchangeRate() {
    Double _rate = this.exchange.getRate();
    return _rate;
  }
  
  public CurrencyType getCurrencyType() {
    CurrencyType _currencyType = this.exchange.getCurrencyType();
    return _currencyType;
  }
  
  /**
   * 人民币金额
   */
  public Double toHostAmount() {
    double _xblockexpression = (double) 0;
    {
      Double exchangeRate = this.exchange.getRate();
      Double _nullsafe = NumberExtensions.nullsafe(this.amount);
      double _multiply = DoubleExtensions.operator_multiply(_nullsafe, exchangeRate);
      _xblockexpression = (_multiply);
    }
    return Double.valueOf(_xblockexpression);
  }
  
  public RCProjectMoney plus(final RCProjectMoney money) {
    RCProjectMoney _rCProjectMoney = new RCProjectMoney();
    final Procedure1<RCProjectMoney> _function = new Procedure1<RCProjectMoney>() {
        public void apply(final RCProjectMoney it) {
          it.exchange = RCProjectMoney.this.exchange;
          Double _nullsafe = NumberExtensions.nullsafe(RCProjectMoney.this.amount);
          Double _rate = RCProjectMoney.this.exchange.getRate();
          Double _amount = money.toAmount(_rate);
          double _plus = DoubleExtensions.operator_plus(_nullsafe, _amount);
          it.amount = Double.valueOf(_plus);
        }
      };
    RCProjectMoney _doubleArrow = ObjectExtensions.<RCProjectMoney>operator_doubleArrow(_rCProjectMoney, _function);
    return _doubleArrow;
  }
  
  public RCProjectMoney substract(final RCProjectMoney money) {
    RCProjectMoney _rCProjectMoney = new RCProjectMoney();
    final Procedure1<RCProjectMoney> _function = new Procedure1<RCProjectMoney>() {
        public void apply(final RCProjectMoney it) {
          it.exchange = RCProjectMoney.this.exchange;
          Double _nullsafe = NumberExtensions.nullsafe(RCProjectMoney.this.amount);
          Double _exchangeRate = it.getExchangeRate();
          Double _amount = money.toAmount(_exchangeRate);
          double _minus = DoubleExtensions.operator_minus(_nullsafe, _amount);
          it.amount = Double.valueOf(_minus);
        }
      };
    RCProjectMoney _doubleArrow = ObjectExtensions.<RCProjectMoney>operator_doubleArrow(_rCProjectMoney, _function);
    return _doubleArrow;
  }
  
  public RCProjectMoney canTake(final RCProjectMoney money) {
    RCProjectMoney _rCProjectMoney = new RCProjectMoney();
    final Procedure1<RCProjectMoney> _function = new Procedure1<RCProjectMoney>() {
        public void apply(final RCProjectMoney it) {
          Double _hostAmount = money.toHostAmount();
          Double _hostAmount_1 = RCProjectMoney.this.toHostAmount();
          boolean _greaterEqualsThan = (_hostAmount.compareTo(_hostAmount_1) >= 0);
          if (_greaterEqualsThan) {
            Double _nullsafe = NumberExtensions.nullsafe(RCProjectMoney.this.amount);
            it.amount = _nullsafe;
          } else {
            Double _nullsafe_1 = NumberExtensions.nullsafe(money.amount);
            it.amount = _nullsafe_1;
          }
          it.exchange = RCProjectMoney.this.exchange;
        }
      };
    RCProjectMoney _doubleArrow = ObjectExtensions.<RCProjectMoney>operator_doubleArrow(_rCProjectMoney, _function);
    return _doubleArrow;
  }
  
  public RCProjectMoney div(final Double value) {
    RCProjectMoney _rCProjectMoney = new RCProjectMoney();
    final Procedure1<RCProjectMoney> _function = new Procedure1<RCProjectMoney>() {
        public void apply(final RCProjectMoney it) {
          it.exchange = RCProjectMoney.this.exchange;
          Double _nullsafe = NumberExtensions.nullsafe(RCProjectMoney.this.amount);
          double _divide = DoubleExtensions.operator_divide(_nullsafe, value);
          it.amount = Double.valueOf(_divide);
        }
      };
    RCProjectMoney _doubleArrow = ObjectExtensions.<RCProjectMoney>operator_doubleArrow(_rCProjectMoney, _function);
    return _doubleArrow;
  }
  
  public RCProjectMoney multi(final Double value) {
    RCProjectMoney _rCProjectMoney = new RCProjectMoney();
    final Procedure1<RCProjectMoney> _function = new Procedure1<RCProjectMoney>() {
        public void apply(final RCProjectMoney it) {
          it.exchange = RCProjectMoney.this.exchange;
          Double _nullsafe = NumberExtensions.nullsafe(RCProjectMoney.this.amount);
          double _multiply = DoubleExtensions.operator_multiply(_nullsafe, value);
          it.amount = Double.valueOf(_multiply);
        }
      };
    RCProjectMoney _doubleArrow = ObjectExtensions.<RCProjectMoney>operator_doubleArrow(_rCProjectMoney, _function);
    return _doubleArrow;
  }
  
  public RCProjectMoney neg() {
    RCProjectMoney _rCProjectMoney = new RCProjectMoney();
    final Procedure1<RCProjectMoney> _function = new Procedure1<RCProjectMoney>() {
        public void apply(final RCProjectMoney it) {
          it.exchange = RCProjectMoney.this.exchange;
          Double _nullsafe = NumberExtensions.nullsafe(RCProjectMoney.this.amount);
          double _minus = DoubleExtensions.operator_minus(_nullsafe);
          it.amount = Double.valueOf(_minus);
        }
      };
    RCProjectMoney _doubleArrow = ObjectExtensions.<RCProjectMoney>operator_doubleArrow(_rCProjectMoney, _function);
    return _doubleArrow;
  }
  
  public RCProjectMoney zero() {
    RCProjectMoney _rCProjectMoney = new RCProjectMoney();
    final Procedure1<RCProjectMoney> _function = new Procedure1<RCProjectMoney>() {
        public void apply(final RCProjectMoney it) {
          it.exchange = RCProjectMoney.this.exchange;
          it.amount = Double.valueOf(0d);
        }
      };
    RCProjectMoney _doubleArrow = ObjectExtensions.<RCProjectMoney>operator_doubleArrow(_rCProjectMoney, _function);
    return _doubleArrow;
  }
  
  public Double toAmount(final Double exchangeRate) {
    double _xifexpression = (double) 0;
    boolean _equals = Objects.equal(this.amount, null);
    if (_equals) {
      _xifexpression = 0d;
    } else {
      Double _nullsafe = NumberExtensions.nullsafe(this.amount);
      Double _exchangeRate = this.getExchangeRate();
      double _multiply = DoubleExtensions.operator_multiply(_nullsafe, _exchangeRate);
      double _divide = (_multiply / (exchangeRate).doubleValue());
      _xifexpression = _divide;
    }
    return Double.valueOf(_xifexpression);
  }
  
  public RCProjectMoney toMoney(final RCExchangeRate exchange) {
    RCProjectMoney _rCProjectMoney = new RCProjectMoney();
    final Procedure1<RCProjectMoney> _function = new Procedure1<RCProjectMoney>() {
        public void apply(final RCProjectMoney it) {
          Double _nullsafe = NumberExtensions.nullsafe(RCProjectMoney.this.amount);
          Double _exchangeRate = RCProjectMoney.this.getExchangeRate();
          double _multiply = DoubleExtensions.operator_multiply(_nullsafe, _exchangeRate);
          Double _rate = exchange.getRate();
          double _divide = (_multiply / (_rate).doubleValue());
          it.amount = Double.valueOf(_divide);
          it.exchange = exchange;
        }
      };
    RCProjectMoney _doubleArrow = ObjectExtensions.<RCProjectMoney>operator_doubleArrow(_rCProjectMoney, _function);
    return _doubleArrow;
  }
  
  public String toString() {
    Double _nullsafe = NumberExtensions.nullsafe(this.amount);
    String _string = _nullsafe.toString();
    String _xifexpression = null;
    CurrencyType _currencyType = this.getCurrencyType();
    String _codeid = _currencyType.getCodeid();
    boolean _equals = _codeid.equals("01");
    if (_equals) {
      _xifexpression = "\u5143";
    } else {
      CurrencyType _currencyType_1 = this.getCurrencyType();
      String _name = _currencyType_1.getName();
      _xifexpression = _name;
    }
    String _plus = (_string + _xifexpression);
    return _plus;
  }
  
  public boolean equals(final Object object) {
    boolean _xblockexpression = false;
    {
      boolean _equals = Objects.equal(object, null);
      if (_equals) {
        return false;
      }
      Double _exchangeRate = this.getExchangeRate();
      Double _amount = ((RCProjectMoney) object).toAmount(_exchangeRate);
      boolean _equals_1 = _amount.equals(this.amount);
      _xblockexpression = (_equals_1);
    }
    return _xblockexpression;
  }
  
  public static RCProjectMoney create() {
    return new RCProjectMoney();
    
    
  }
}
